package com.p2p.base.page;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.p2p.base.util.DateUtil;

/**
 * 分页查询Bean
 * 
 * @author fansunion@qq.com 2014年11月2日
 */
public class PageVo {

	private Logger log = Logger.getLogger(this.getClass());

	/** 总记录数 */
	private Integer totalCount = 0;
	/** 总共有多少页 */
	private Integer pages = 0;
	/** 第几页 */
	private Integer pageNum = 1;
	/** 每页显示条数 */
	private Integer pageSize = 10;

	// 兼容后端
	private Integer rows;
	// 兼容后端
	private Integer page;

	// 外部排序参数
	/** 排序字段 */
	private String orderBy;
	/** 升序还是降序 asc,desc */
	private String order;

	/** 其它额外的输入参数，将作为分页超链接的查询参数(精确或模糊) */
	private Map<String, String> params = new HashMap<String, String>();

	/** 表格中的数据 */
	private List<?> list;
	/** 其它额外的输出数据 */
	private Map<String, Object> otherData = new HashMap<String, Object>();

	private String startDate;

	private String endDate;

	private String copyStartDate;
	private String copyEndDate;

	private String search;
	private String copySearch;

	public PageVo() {

	}

	// 防止被攻击，比如把pageSize设置成10000，每页就显示10000条数据，给数据库查询造成很大压力
	public void check() {
		if (pageSize > 100) {
			log.error("pageSize is so big,pageSize=" + pageSize);
			pageSize = 10;
		}
		if (pageNum < 1) {
			log.error("pageNum is so small,pageNum=" + pageNum);
			pageNum = 1;
		}
	}

	@Deprecated
	public PageVo(PageVo vo, List<?> list) {
		this.pageNum = vo.getPageNum();
		this.pageSize = vo.getPageSize();
		this.totalCount = vo.getTotalCount();
		this.pages = vo.getPages();
		this.list = list;
		this.params = vo.getParams();
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public Integer getFirstResult() {
		return (getPageNum() - 1) * getPageSize();
	}

	public Integer getMaxResults() {
		return getPageSize();
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public final Integer getTotalCount() {
		return totalCount;
	}

	public final void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.pageSize = rows;
		this.rows = rows;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
		this.pageNum = page;
	}

	public List<?> getList() {
		return list;
	}

	public void setList(List<?> list) {
		this.list = list;
	}

	public Integer getPages() {
		return pages;
	}

	public void setPages(Integer totalPage) {
		this.pages = totalPage;
	}

	public void setParams(Map<String, String> otherData) {
		this.params = otherData;
	}

	public Map<String, String> getParams() {
		return params;
	}

	public Map<String, Object> getOtherData() {
		return otherData;
	}

	public void setOtherData(Map<String, Object> otherData) {
		this.otherData = otherData;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	@Override
	public String toString() {
		return "PageVo [totalCount=" + totalCount + ", pages=" + pages
				+ ", pageNum=" + pageNum + ", pageSize=" + pageSize + ", rows="
				+ rows + ", orderBy=" + orderBy + ", order=" + order
				+ ", params=" + params + ", list=" + list + ", otherData="
				+ otherData + ", startDate=" + startDate + ", endDate="
				+ endDate + ", copyStartDate=" + copyStartDate
				+ ", copyEndDate=" + copyEndDate + ", search=" + search + "]";
	}

	// 输入
	public void handleIntDate() {
		buildStartDate();
		buildEndDate();
		try {
			startDate = DateUtil.parseDateTime(startDate).getTime() + "";
			endDate = DateUtil.parseDateTime(endDate).getTime() + "";
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}

	public void handleDatetimeDate() {
		buildStartDate();
		buildEndDate();
	}

	private void buildStartDate() {
		// 处理之前先备份用户输入的日期数据，页面回显需要
		this.copyStartDate = startDate;
		if (StringUtils.isEmpty(startDate)) {
			// 要足够小
			startDate = "1990-1-1";
		}
	
		if (StringUtils.isNotBlank(startDate) && startDate.length() < DATE_FORMAT_LENGTH) {
			// 用户只需要输入“年-月-日”，“2014-11-11”，程序自动补齐 “时分秒”，“00:00:00”
			startDate += " 00:00:00";
		}
	}
	
	public final int DATE_FORMAT_LENGTH = "yyyy-MM-dd HH:mm:ss".length();

	private void buildEndDate() {
		this.copyEndDate = endDate;
		if (StringUtils.isEmpty(endDate)) {
			// 要足够大
			endDate = "2100-1-1";
		}
		if (StringUtils.isNotBlank(endDate) && endDate.length() < 14) {
			// 用户只需要输入“年-月-日”，“2014-11-11”，程序自动补齐 “时分秒”，“00:00:00”
			endDate += " 23:59:59";
		}
	}

	/**
	 * 数据库的日期类型可能是 bigint 或者 dateime
	 * 
	 * @param isDate
	 *            是否是 datetime
	 */
	// 处理日期
	@Deprecated
	public void handleDate(boolean isBigint) {
		// 处理之前先备份用户输入的日期数据，页面回显需要
		this.copyStartDate = startDate;
		this.copyEndDate = endDate;
		// 默认的起止时间是：0~100000000000000000，这样日期就没有限制了
		// 时间是int类型的
		if (StringUtils.isEmpty(startDate)) {
			startDate = "0";
		} else {
			try {
				// 用户只需要输入“年-月-日”，“2014-11-11”，程序自动补齐 “时分秒”，“00:00:00”
				startDate += " 00:00:00";
				if (isBigint) {
					startDate = DateUtil.parseDateTime(startDate).getTime()
							+ "";
				}

			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if (StringUtils.isEmpty(endDate)) {
			// 要足够大
			endDate = "100000000000000000";
		} else {
			try {
				endDate += " 23:59:59";
				if (isBigint) {
					endDate = DateUtil.parseDateTime(endDate).getTime() + "";
				}

			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
	}

	public void handleSearch() {
		if (StringUtils.isNotBlank(search)) {
			this.copySearch = search;
			this.search = "%" + search + "%";
		}
	}

	public void resetSearch() {
		this.search = copySearch;
	}

	public void resetDate() {
		startDate = copyStartDate;
		endDate = copyEndDate;

	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

}